package inet.app.view.activity;



import inet.app.model.util.Util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;

public class VesoService extends Service {

//	private static final String TAG = "BroadcastService";
	public static final String BROADCAST_ACTION = "service_veso";
	private final Handler handler = new Handler();
	Intent intent;
	
	@Override
	public void onCreate() {
		super.onCreate();

		intent = new Intent(BROADCAST_ACTION);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		handler.removeCallbacks(sendUpdatesToUI);
		handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

	}

	private Runnable sendUpdatesToUI = new Runnable() {
		public void run() {
				DisplayLoggingInfo();
				handler.postDelayed(this, 10000); // 1 seconds
		}
	};
	private void DisplayLoggingInfo() {
		Socket s = null;
		DataInputStream _inFromMaster = null;
		byte[] buff = null;
		int buffLength;
		int bs;
		String strResult = "";
		// code inputstream
		PrintWriter out = null;
        BufferedReader in = null;
        // endcode
		try {
			s = ((MyApp) getApplication()).get_socket();
			_inFromMaster = new DataInputStream(s.getInputStream());
			while (_inFromMaster.available() > 0) {
				bs = _inFromMaster.available(); 
				buff = new byte[bs];
				buffLength = buff.length;
				_inFromMaster.read(buff, 0, buffLength);
				strResult = strResult + new String(buff);
//				strResult ="XSSYNC_RESP|0|07/12/2013 10:10:17|XSTD|06/12/2013 18:24:09|1:71563|2:38328-30031|3:30167-15518-64844-44379-96172-30523|4:2638-6471-9699-3504|5:8844-6020-9822-9467-6544-6270|6:706-722-959|7:02-75-88-92|8:|0:66666||\r\n";
//				strResult ="XSSYNC_RESP|0|07/12/2013 10:10:17|XSVL|06/12/2013 18:24:09|1:71563|2:38328|3:30167-15518|4:2638-6471-9699-3504-64844-44379-96172|5:8844|6:7036-7223-9594|7:022|8:85|0:666666||\r\n";
//				try {
//					Thread.sleep(20000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			}
			if(strResult!=null && !"".equalsIgnoreCase(strResult.trim())&&(Util.onLiveTimeMN()|Util.onLiveTimeMT())|Util.onLiveTime()){
				intent.putExtra("broad", strResult);
				System.out.println("send--------------------------------------------"+strResult);
				sendBroadcast(intent);
				strResult = "";
			}
			} catch (Exception e) {
				// e.printStackTrace();
			}
		 }


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		System.out.println("Killl Service Vé số");
//		sendUpdatesToUI=null;
		handler.removeCallbacks(sendUpdatesToUI);
		super.onDestroy();
	}
}